package com.wang.vedio.player;

import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;
import com.vondear.rxtool.RxActivityTool;
import com.vondear.rxtool.view.RxToast;
import com.wang.vedio.MainActivity2;
import com.wang.vedio.R;
import com.wang.vedio.adapter.index.SearchAdapter;
import com.wang.vedio.base.BaseActivity;
import com.wang.vedio.net.SharedHelper;
import com.wang.vedio.net.entity.BaseBean;
import com.wang.vedio.net.entity.LoginBean;
import com.wang.vedio.net.entity.VideoBean;
import com.wang.vedio.net.rxjava.HttpMethods;
import com.wang.vedio.net.subscribers.ProgressSubscriber;
import com.wang.vedio.net.subscribers.SubscriberOnNextListener;
import com.wang.vedio.utils.StringUtils;
import com.wang.vedio.utils.statusbar.StatusBarUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.MediaType;
import okhttp3.RequestBody;

public class PasswordActivity extends BaseActivity {
    @BindView(R.id.label)
    TextView label;
    @BindView(R.id.rl_center)
    View rl_center;


    @BindView(R.id.p1)
    TextView p1;
    @BindView(R.id.p2)
    TextView p2;
    @BindView(R.id.p3)
    TextView p3;
    @BindView(R.id.p4)
    TextView p4;
    @BindView(R.id.tgBtn)
    TextView tgBtn;
    @BindView(R.id.sureBtn)
    TextView sureBtn;
    @BindView(R.id.b0)
    TextView b0;
    @BindView(R.id.b1)
    TextView b1;
    @BindView(R.id.b2)
    TextView b2;
    @BindView(R.id.b3)
    TextView b3;
    @BindView(R.id.b4)
    TextView b4;
    @BindView(R.id.b5)
    TextView b5;
    @BindView(R.id.b6)
    TextView b6;
    @BindView(R.id.b7)
    TextView b7;
    @BindView(R.id.b8)
    TextView b8;
    @BindView(R.id.b9)
    TextView b9;
    @BindView(R.id.bdel)
    TextView bdel;


    @BindView(R.id.czanLinear)
    View czanLinear;

    @BindView(R.id.czBtn)
    View czBtn;


    @BindView(R.id.szaqmmLinear)
    View szaqmmLinear;
    @BindView(R.id.xgmmLinear)
    View xgmmLinear;

    @BindView(R.id.oldEdit)
    EditText oldEdit;
    @BindView(R.id.newEdit1)
    EditText newEdit1;
    @BindView(R.id.newEdit2)
    EditText newEdit2;


    private String inputInfo = "";

    private ArrayList<VideoBean> mList = new ArrayList<>();
    private SearchAdapter mAdapter;

    private TextView[] inputTv = new TextView[4];
    private TextView[] btnTv = new TextView[12];

    @Override
    protected int getLayoutId() {
        return R.layout.gyss_password_layout;
    }

    private int type;

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        type = getIntent().getExtras().getInt("type");
        if (type == 0) {
            tgBtn.setVisibility(View.VISIBLE);
            sureBtn.setVisibility(View.VISIBLE);
            label.setText("设置安全密码");
            rl_center.setVisibility(View.INVISIBLE);
        } else if (type == -1) {
            if (StringUtils.isEmpty(SharedHelper.readValueByKeyForString(mContext, "password"))) {
                xgmmLinear.setVisibility(View.GONE);
                szaqmmLinear.setVisibility(View.VISIBLE);
                setTitle("设置隐私密码");
                rl_center.setVisibility(View.VISIBLE);
                tgBtn.setVisibility(View.GONE);
                sureBtn.setVisibility(View.VISIBLE);
                label.setText("设置安全密码");
            } else {
                setTitle("修改密码");
                xgmmLinear.setVisibility(View.VISIBLE);
                szaqmmLinear.setVisibility(View.GONE);
                tgBtn.setVisibility(View.GONE);
            }
        } else {
            tgBtn.setVisibility(View.INVISIBLE);
            sureBtn.setVisibility(View.INVISIBLE);
            rl_center.setVisibility(View.INVISIBLE);
            label.setText("输入安全密码");
        }
        StatusBarUtil.setStatusBar(this, true, R.color.transparent, false, true);
        init();
    }

    private void init() {
        TextView[] inputTvss = {p1, p2, p3, p4};
        inputTv = inputTvss;

        TextView[] btnTvsss = {b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, bdel};
        btnTv = btnTvsss;
    }


    @Override
    protected void initData() {
    }

    private int page = 1;

    private void savePassword(String password) {
        SubscriberOnNextListener<BaseBean<LoginBean>> onNextListener = baseBean -> {
            SharedHelper.saveKeyValueForString(mContext, "ncName", baseBean.getData().getNcName());
            SharedHelper.saveKeyValueForString(mContext, "inviteNumber", baseBean.getData().getInviteNumber());
            SharedHelper.saveKeyValueForString(mContext, "password", baseBean.getData().getPassword());
            if (type == 0) {
                RxActivityTool.skipActivity(mContext, MainActivity2.class);
            }
            RxToast.normal("操作成功！");
            finish();
        };
        Map<String, Object> params = new HashMap<>();
        params.put("id", SharedHelper.readId(mContext));
        params.put("password", password);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=utf-8"), new Gson().toJson(params));
        HttpMethods.getInstance().loginApp(new ProgressSubscriber<>(onNextListener, mContext, true), requestBody);
    }

    @OnClick({R.id.sureBtn, R.id.tgBtn, R.id.b0, R.id.b1, R.id.b2, R.id.b3, R.id.b4, R.id.b5, R.id.b6, R.id.b7, R.id.b8, R.id.b9, R.id.bdel, R.id.czBtn})
    public void onClick(View v) {
        if (v.getId() == R.id.tgBtn) {
            RxActivityTool.skipActivity(mContext, MainActivity2.class);
            finish();
        } else if (v.getId() == R.id.sureBtn) {
            if (type == -1 && xgmmLinear.getVisibility() == View.VISIBLE) {
                //修改密码
                if (StringUtils.isEmpty(oldEdit.getText().toString())) {
                    RxToast.normal("请输入原密码");
                    return;
                }
                if (StringUtils.isEmpty(newEdit1.getText().toString())) {
                    RxToast.normal("请输入新密码");
                    return;
                }
                if (StringUtils.isEmpty(newEdit2.getText().toString())) {
                    RxToast.normal("请再次输入新密码");
                    return;
                }
                if (!newEdit2.getText().toString().equals(newEdit1.getText().toString())) {
                    RxToast.normal("两次新密码不一致");
                    return;
                }
                if (newEdit2.getText().toString().length() != 4) {
                    RxToast.normal("新密码长度必须是4位");
                    return;
                }
                if (!oldEdit.getText().toString().equals(SharedHelper.readValueByKeyForString(mContext, "password"))) {
                    RxToast.normal("原密码错误");
                    return;
                }
                savePassword(newEdit2.getText().toString());
                return;
            }
            if (inputInfo.length() < 4) {
                RxToast.normal("请输入4位密码");
                return;
            }
            savePassword(inputInfo);
        } else if (v.getId() == R.id.czBtn) {
            inputInfo = "";
            p1.setText("");
            p2.setText("");
            p3.setText("");
            p4.setText("");
            return;
        } else if (v.getId() == R.id.bdel) {
            if (inputInfo.length() < 2) {
                inputInfo = "";
                p1.setText("");
                p2.setText("");
                p3.setText("");
                p4.setText("");
                return;
            }
            inputTv[inputInfo.length() - 1].setText("");
            inputInfo = inputInfo.substring(0, inputInfo.length() - 1);
            return;
        } else {
            if (inputInfo.length() == 4) {
                return;
            }
            TextView btn = (TextView) v;
            if (inputInfo.length() == 3) {
                inputInfo += btn.getText().toString();
                inputTv[inputInfo.length() - 1].setText(btn.getText().toString());
                if (type > 0) {
                    if (!inputInfo.equals(SharedHelper.readValueByKeyForString(mContext, "password"))) {
                        RxToast.normal("密码错误，请重新输入！");
                        inputInfo = "";
                        inputTv[0].setText("");
                        inputTv[1].setText("");
                        inputTv[2].setText("");
                        inputTv[3].setText("");
                        return;
                    }

                    //校验成功，进入首页
                    RxActivityTool.skipActivity(mContext, MainActivity2.class);
                    finish();
                    return;
                }
            }else{
                inputInfo += btn.getText().toString();
                inputTv[inputInfo.length() - 1].setText(btn.getText().toString());
            }
        }
    }
}
